/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author scriptoff
 */
public class View {

    private JFrame f;
    private ActionListener aL;
    private WindowListener wL;
    private JButton demandeNombreAnneeButton;
    private JLabel montantAnnuitLabel;
    private JTextField montantAnnuitTextField;
    private JLabel tauxPretLabel;
    private JTextField tauxPretTextField;
    private JLabel montantPretLabel;
    private JTextField montantPretTextField;
    private JLabel nombreAnneeLabel;
    private JButton demandeAnnuiteeButton;
    private JTextField nombreAnneeTextField;
    public final static String DMD_ANNUIT = "DemandeAnnuit";
    public final static String DMD_NB_ANNEE = "DemandeAnnee";
    private ResourceBundle rB = null;

    public View(Controleur c) {
        this.rB = c.getResourceBundle();
        f = new JFrame();
        f.setTitle(rB.getString("CECI EST UNE REVOLUTION"));
        initComponent(f.getContentPane());
        f.pack();
        f.setLocationRelativeTo(null);
    }

    public void setaL(ActionListener aL) {
        this.aL = aL;
        abonnerButton();
    }

    public void setwL(WindowListener wL) {
        this.wL = wL;
        abonnerFenetre();
    }
    

    private void initComponent(Container pane) {
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;

        montantAnnuitLabel = new JLabel(rB.getString("MONTANT ANNUITÉE : "));
        c.gridx = 0;
        c.gridy = 4;
        pane.add(montantAnnuitLabel, c);

        montantAnnuitTextField = new JTextField();
        c.gridx = 1;
        c.gridy = 4;
        pane.add(montantAnnuitTextField, c);

        tauxPretLabel = new JLabel(rB.getString("TAUX DU PRÊT : "));
        c.gridx = 0;
        c.gridy = 2;
        pane.add(tauxPretLabel, c);

        tauxPretTextField = new JTextField();
        c.gridx = 1;
        c.gridy = 2;
        pane.add(tauxPretTextField, c);

        montantPretLabel = new JLabel(rB.getString("MONTANT DU PRÊT : "));
        c.gridx = 0;
        c.gridy = 0;
        pane.add(montantPretLabel, c);

        montantPretTextField = new JTextField();
        c.gridx = 1;
        c.gridy = 0;
        pane.add(montantPretTextField, c);

        nombreAnneeLabel = new JLabel(rB.getString("NOMBRE D'ANNÉE : "));
        c.gridx = 0;
        c.gridy = 3;
        pane.add(nombreAnneeLabel, c);

        nombreAnneeTextField = new JTextField();
        c.gridx = 1;
        c.gridy = 3;
        pane.add(nombreAnneeTextField, c);

        demandeAnnuiteeButton = new JButton(rB.getString("DEMANDE ANNUITÉE"));
        c.gridx = 0;
        c.gridy = 5;
        pane.add(demandeAnnuiteeButton, c);

        demandeNombreAnneeButton = new JButton(rB.getString("DEMANDE NOMBRE ANNEE"));
        c.gridx = 1;
        c.gridy = 5;
        pane.add(demandeNombreAnneeButton, c);

    }
    
    public void abonnerButton() {
        demandeAnnuiteeButton.setActionCommand(DMD_ANNUIT);
        demandeAnnuiteeButton.addActionListener(aL);
        demandeNombreAnneeButton.setActionCommand(DMD_NB_ANNEE);
        demandeNombreAnneeButton.addActionListener(aL);
        
    }

    private void abonnerFenetre() {
        f.addWindowListener(wL);
    }

    public void go() {
        f.setVisible(true);
    }
    
/*    public static void main(String[] args) {
        Controleur c = new Controleur();
        View v = new View();
        c.setView(v);
        ModeleLocal m = new ModeleLocal();
        c.setInterfaceModele(m);
        v.setaL(c);
        v.setwL(c);
        v.go();

    }*/

    public String getMontantPret() {
        return montantPretTextField.getText();
    }

    public String getTaux() {
        return tauxPretTextField.getText();
    }

    public String getNbAnnee() {
        return nombreAnneeTextField.getText();
    }

    public String getAnnuit() {
        return montantAnnuitTextField.getText();
    }

    public void drawErrorDialog(String msg) {
        JOptionPane.showMessageDialog(null, msg, rB.getString("ERREUR"), JOptionPane.ERROR_MESSAGE);
    }

    public void drawResultDialog(double result) {
        JOptionPane.showMessageDialog(null, rB.getString("LE RESULTAT EST : ") + Double.toString(result), rB.getString("RESULTAT") , JOptionPane.INFORMATION_MESSAGE);
    }

}
